package edu.bit.water.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import edu.bit.water.entity.LabelEvaCmpWaterTAndASum;

import edu.bit.water.service.LabelEvaCmpWaterAnlService;
import edu.bit.water.util.FormulaIntermediateVariable;


/**
 * (水平衡)企业用水分析标签表控制层
 * @author HJS
 *
 */
@Controller
@RequestMapping("/label_eva_cmp_water_anl")
public class LabelEvaCmpWaterAnlController {
	
	@Autowired
	private LabelEvaCmpWaterAnlService labelEvaCmpWaterAnlService;
	
	private static final Logger log = LoggerFactory.getLogger(LabelEvaCmpWaterAnlController.class);
	
	/**
	 * 
	 * @param labelEvaCmpWaterAnl 用水分析表标签表
	 * @return 成功返回1，失败返回0
	 */
	@ResponseBody
	@RequestMapping("/insert")
    public Map<String, String> insert(LabelEvaCmpWaterTAndASum labelEvaCmpWaterAnl){
		Map<String, String> map = new HashMap<>();
		int n = labelEvaCmpWaterAnlService.add(labelEvaCmpWaterAnl);
		if(n == 1)
			map.put("status", "success");
		else
			map.put("status", "failed");
		return map;
	}
	/**
	 * 
	 * @param id 用水分析表标签表的序列号
	 * @return 成功返回所有记录，失败返回null
	 */
	@ResponseBody
	@RequestMapping("/select")
    public Map<String, Object> select(){
		Map<String, Object> map = new HashMap<>();
		List<LabelEvaCmpWaterTAndASum> list = labelEvaCmpWaterAnlService.select();
		if(list != null) {
			map.put("status", "success");
			map.put("rows", list);
		}
		else {
			map.put("status", "failed");
			map.put("msg", "暂时无记录");
		}
		return map;
	}
	/**
	 * 
	 * @param idLists 用水分析表标签表id集合
	 * @return 成功返回1，失败返回0
	 */
	@ResponseBody
	@RequestMapping("/delete")
    public Map<String, String> delete(@RequestBody List<Integer> idLists){
		Map<String, String> map = new HashMap<>();
		int n = labelEvaCmpWaterAnlService.delete(idLists);
		if(n == 1)
			map.put("status", "success");
		else
			map.put("status", "failed");
		return map;
	}
	/**
	 * 
	 * @param labelEvaCmpWaterAnl 用水分析表标签表
	 * @return 成功返回1，失败返回0
	 */
	@ResponseBody
	@RequestMapping("/update")
	public Map<String, String> update(LabelEvaCmpWaterTAndASum labelEvaCmpWaterAnl) {
		Map<String, String> map = new HashMap<>();
		int n = labelEvaCmpWaterAnlService.update(labelEvaCmpWaterAnl);
		if(n == 1)
			map.put("status", "success");
		else
			map.put("status", "failed");
		return map;
	}
	/**
	 * 
	 * @param id 标签表id
	 * @return 中间变量
	 */
	@ResponseBody
	@RequestMapping("/selectMiddleVariable")
	public Map<String, Object> selectMiddleVariable(int id) {
		Map<String, Object> map = new HashMap<>();
		FormulaIntermediateVariable formulaIntermediateVariable = labelEvaCmpWaterAnlService.selectMiddleVariable(id);
		map.put("status", "success");
		map.put("rows", formulaIntermediateVariable);
		return map;
	}
	/**
	 * 
	 * @param formulaIntermediateVariable 用于保存中间参数的实体类
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateVariable")
	public Map<String, String> updateVariable(FormulaIntermediateVariable formulaIntermediateVariable) {
		formulaIntermediateVariable.showContent();
		Map<String, String> map = new HashMap<>();
		int n = labelEvaCmpWaterAnlService.updateVariable(formulaIntermediateVariable);
		if (n == 1) {
			map.put("status", "success");
		}
		else {
			map.put("status", "failed");
		}
		return map;
	}
	/**
	 * 
	 * @param id
	 * @return 返回计算值和统计值
	 */
	@ResponseBody
	@RequestMapping("/selectSumResult")
	public Map<String, Object> selectSumResult(Integer id) {
		Map<String, Object> map = new HashMap<>();
		try {
			LabelEvaCmpWaterTAndASum labelEvaCmpWaterTAndASum = labelEvaCmpWaterAnlService.selectSumResult(id);
			if (labelEvaCmpWaterTAndASum != null) {
				map.put("status", "success");
				map.put("rows", labelEvaCmpWaterTAndASum);
			}
			else {
				map.put("status", "failed");
			}
		}catch (Exception e) {
			map.put("status", "failed");
			map.put("msg", "result="+e.getMessage());
			e.printStackTrace();
		}
		
		return map;
	}
}